<template>
  <div class="auto">
    <validator name='v'>
      <form novalidate class="form-horizontal">
        <p class="bg-info text-center" style="padding: 8px;">其他补气信息</p>
        <div class="row" v-if="isGas">
          <label for="f_pregas" class=" col-sm-2 control-label">应补气量:</label>
          <div class="col-sm-2">
            <input  class="form-control" type="number" min = "0" v-model="model.f_pregas" placeholder="气量">
          </div>
        </div>
        <div class="row" v-if="!isGas">
          <label for="f_preamount" class=" col-sm-2 control-label">应补金额:</label>
          <div class="col-sm-2">
            <input  class="form-control" type="number" min = "0" v-model="model.f_preamount" placeholder="金额">
          </div>
        </div>
      </form>
      <print-bill :show="print" v-ref:printbill v-on:toggle="close" :data='row'></print-bill>
      <!-- <tax-other-bill :show="taxprint"  v-ref:taxprintbill :row='row' :logic='model' v-on:success="close" v-on:toggle="close"></tax-other-bill> -->
      <div style="text-align:right;margin-top:30px;margin-right:50px;">
        <button class="btn btn-success" @click="confirm()"  :disabled='!$v.valid' id='components-comOpe-othCha-确认'>确&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;认</button>
        <button class="btn btn-default" @click="clean()" style="width:80px;margin-left:20px;" id='components-comOpe-othCha-取消'>取&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;消</button>
      </div>
    </validator>
  </div>
</template>
<script>
// import AppData from '../../../../../stores/AppData'
import * as Util from '../../../../Util'
// import co from 'co'

let otherGasGen = async function(self) {
  // 划价
  let data = self.getSell()
  // 组织划价model
  let param = {
    f_price_id: self.row.f_price_id,
    f_userinfo_id: self.row.f_userinfo_id,
    f_meter_type: self.row.f_meter_type
  }
  // 若是气量卡表 temp为金额  若是金额卡表 temp为气量
  if (self.isGas) {
    data.f_pregas = self.model.f_pregas
    let fee = await self.$CommonService.gasCalculate(param, self.model.f_pregas)
    data.f_preamount = fee.data.chargenum
  } else {
    data.f_preamount = self.model.f_preamount
    let gas = await self.$CommonService.feeCalculate(param, self.model.f_preamount)
    data.f_pregas = gas.data.gas
  }
  // 计算本期底数
  data.f_tablebase = (self.row.f_meter_base - 0) + (data.f_pregas - 0),

  data.userinfo = {
    f_userinfo_id: self.row.f_userinfo_id,
    version: self.row.userinfo_version
  }
  data.userinfo.userfiles = {
    f_user_id: self.row.f_user_id,
    f_meter_base: (self.row.f_meter_base - 0) + (data.f_pregas - 0),
    f_total_fee: (self.row.f_total_fee - 0) + (data.f_preamount - 0),
    f_times: self.row.cardInfo.Times + 1,
    version: self.row.version
  }

  let result = await self.$resetpost('rs/entity/t_sellinggas', data, {resolveMsg: null, rejectMsg: null})

  // 写卡操作
  let cardParams = await self.$resetpost('rs/logic/stairCard', {data: {f_card_id: self.row.f_card_id + '', f_alias: self.row.f_alias}}, {resolveMsg: null, rejectMsg: '获取写卡参数失败'})

  // 获取写卡参数
  let params = self.getWriteCard(cardParams, self.row, data)

  let cardRes = await self.$resetpost('http://127.0.0.1:8003/WriteGasCard', params, {resolveMsg: null, rejectMsg: '补气卡服务错误'})

  // 卡服务写卡异常，业务逻辑回滚
  if (!cardParams || !result || cardRes.data.Err || cardRes.data.Exception) {
    let data = {
      userfile: {
        f_user_id: self.row.f_user_id,
        f_meter_base: self.row.f_meter_base
      },
      id: result.data.id
    }
    await self.$resetpost('rs/logic/otherGasRollback', {data: data}, {resolveMsg: '补气失败', rejectMsg: '补气失败'})
  } else {
    let record = {
      id: result.data.id,
      f_state: '有效'
    }
    return await self.$resetpost('rs/entity/t_record', record, {resolveMsg: '补气成功', rejectMsg: '补气修改状态失败'})
  }
}

export default {
  title: '其他补气',
  data () {
    return {
      isGas: false,
      print: false
    }
  },
  props: ['row'],
  ready () {
    console.log('其他补气，，，', this.row)
    this.isGas = this.row.f_meter_type === '气量卡表'
  },
  watch: {
  },
  methods: {
    confirm () {
      otherGasGen(this).then((res) => {
        console.log('保存成功!!', res)
        this.row.id = res.data.id
        this.row.f_bill_type = '卡表收费'
        this.row.f_bill_style = '专用收据'
        this.row.f_preamount = this.model.f_preamount
        this.row.f_collection = 0
        this.print = true
        this.$refs.printbill.cardbill.search(res.data.id)
      })
    },
    getSell () {
      return {
        f_last_tablebase: this.row.f_meter_base,
        f_tablebase: (this.row.f_meter_base - 0) + (this.model.f_pregas - 0),
        f_balance: this.row.f_balance,
        f_curbalance: this.row.f_balance,
        f_card_id: this.row.f_card_id,
        f_meter_type: this.row.f_meter_type,
        f_payment: this.row.f_cost_type,
        f_price_id: this.row.f_price_id,
        f_address: this.row.f_address,
        f_meter_brand: this.row.f_meter_brand,
        f_meter_style: this.row.f_meter_style,
        f_meternumber: this.row.f_meternumber,
        f_user_type: this.row.f_user_type,
        f_gasproperties: this.row.f_gasproperties,
        f_collection: 0,
        f_delaypay: 0,
        f_charge_type: '其他补气',
        f_charge_state: '有效',
        f_user_id: this.row.f_user_id,
        record_userinfo: this.row.f_userinfo_id,
        f_userinfo_id: this.row.f_userinfo_id,
        f_user_name: this.row.f_user_name,
        f_operat_type: '卡表收费',
        f_describe: `${Util.f.name}对用户${this.row.f_user_name}进行其他补气操作`,
        f_state: '未知',
        f_operator: Util.f.name,
        f_filiale: Util.f.f_fengongsi,
        f_outlets: Util.f.f_parentname
      }
    },
    getWriteCard (cardParams, row, data) {
      let params = {
        factory: row.f_alias,                                // 气表厂家
        kh: row.f_card_id,                                   // 卡号
        kmm: row.f_card_password,                            // 卡密码，写卡后返回新密码, 不论补卡还是先开户都没有密码
        bjql: row.f_police_gas,                              // 报警气量,t_gasbrand表中
        czsx: row.f_topup_ceil,                              // 充值上线，t_gasbrand表中
        tzed: row.f_overdr_lines,                            // 透支额度，t_gasbrand表中
        bkcs: '0',                                          // 补卡次数，从补卡表里通过表编号获得
        ql: data.f_pregas,                            // 预购气量
        cs: row.cardInfo.Times + 1,                                     // 求购气次数，先开户为1
        money: data.f_preamount,                            // 购气金额
        klx: row.f_coding
      }
      return Object.assign({}, this.$CommonService.writeCardParams(cardParams), params)
    },
    close () {
      this.print = false
      this.clean()
    },
    clean () {
      this.$info('取消操作')
      this.$dispatch('cancelclean', this.row)
    },
  },
  computed: {
  }
}
</script>

<style>
</style>
